﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Qizx.XQuery;
using System.Qizx.XQuery.Fn;
using System.Qizx.XQuery.Dt;
using Signature = System.Qizx.XQuery.Fn.UserFunction.Signature;

namespace System.Qizx.XQuery.Op {
    public class InlineFunction : Expression {
        Signature prototype;

        public InlineFunction(Signature prototype) {
            this.prototype = prototype;
        }

        public override Expression Child(int rank) {
            return rank == 0 ? prototype : null;
        }

        public void dump(ExprDisplay d) {
            d.header(this);
            d.child(prototype);
        }

        public Expression staticCheck(ModuleContext context, int flags) {
            // check the body of the function: the "level" of the local context
            // is changed so that var references to closure context can be detected
            context.pushLocalsLevel();
            context.staticCheck(prototype, 0);
            context.popLocalsLevel();
            setType(new FunctionType(prototype));
            return this;
        }

        public XQValue eval(Focus focus, EvalContext context) {
            return new FunctionItem(prototype, context);
        }
    }
}